﻿using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace WaterWay.Utilities
{
    /// <summary>
    /// Log4Net日志类
    /// 版本：2.0
    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogType { Error, Warning, Running, RecvInfo, HighLevel, LowLevel, StrategyAlarmLog };


    /// <summary>
    /// 日志操作类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 指示是否设置配置文件。
        /// </summary>
        private static bool IsSetConfig = false;
        private static readonly object obj = new object();

        /// <summary>
        /// 异常日志文件操作
        /// </summary>
        private static readonly log4net.ILog _LogError = log4net.LogManager.GetLogger("LogError");

        /// <summary>
        /// 运行消息日志文件操作
        /// </summary>
        private static readonly log4net.ILog _LogRunningInfo = log4net.LogManager.GetLogger("LogRunningInfo");


        private static readonly log4net.ILog _LogWarning = log4net.LogManager.GetLogger("LogWarning");

        private static LogHelper logcurrent;
        public static LogHelper Current()
        {
            if (logcurrent == null)
            {
                lock (obj)
                {
                    if (logcurrent == null)
                    {
                        logcurrent = new LogHelper();
                    }
                }
            }
            return logcurrent;
            //get { return LogHelper.logcurrent; }
        }
        private LogHelper()
        {
            SetConfig();
        }
        public void SetConfig()
        {
            lock (obj)
            {
                if (!IsSetConfig)
                {
                    string path = System.Web.HttpContext.Current.Server.MapPath("XmlConfig/log4net.config");
                    FileInfo ConfigFile = new FileInfo(path);
                    log4net.Config.XmlConfigurator.Configure(ConfigFile);
                    IsSetConfig = true;
                }
            }
        }

        public void Error(Exception ex)
        {
            Error("执行报错", ex);
        }
        public void Error(object msg)
        {
            //_LogError.Error("异常信息（ZIT.Utilities）：" + msg);
            Error(msg.ToString(), null);
        }
        /// <summary>
        /// 写异常日志函数
        /// </summary>
        /// <param name="strMsg"></param>
        /// <param name="ex"></param>
        public void Error(string strMsg, Exception ex)
        {
            _LogError.Error("异常信息（ZIT.Utilities）：" + strMsg, ex);
        }

        public void LogInfo(string meg)
        {
            WriteLog(meg, LogType.Running);
        }
        public void LogWarn(string meg)
        {
            WriteLog(meg, LogType.Warning);
        }

        /// <summary>
        /// 写指定类型的消息日志
        /// </summary>
        /// <param name="strLevelMsg"></param>
        /// <param name="IsHighLevel"></param>
        private void WriteLog(string strLevelMsg, LogType lt)
        {

            switch (lt)
            {
                case LogType.Error:
                    {
                        _LogError.Error("异常信息：" + strLevelMsg);
                        break;
                    }
                case LogType.Warning:
                    {
                        _LogWarning.Warn(strLevelMsg);
                        break;
                    }
                case LogType.Running:
                    {
                        _LogRunningInfo.Info(strLevelMsg);
                        break;
                    }


                default:
                    break;
            }



        }

    }



}



//using log4net;
//using System;
//using System.Collections.Generic;
//using System.IO;
//using System.Linq;
//using System.Text;

//namespace ZIT.Utilities
//{
//    /// <summary>
//    /// Log4Net日志类
//    /// 版本：2.0
//    /// <summary>
//    /// 日志类型
//    /// </summary>
//    public enum LogType { Error, Warning, Running, RecvInfo, HighLevel, LowLevel, StrategyAlarmLog };


//    /// <summary>
//    /// 日志操作类
//    /// </summary>
//    public sealed class LogHelper
//    {
//        /// <summary>
//        /// 指示是否设置配置文件。
//        /// </summary>
//        private static bool IsSetConfig = false;

//        /// <summary>
//        /// 异常日志文件操作
//        /// </summary>
//        private static readonly log4net.ILog _LogError = log4net.LogManager.GetLogger("LogError");

//        /// <summary>
//        /// 运行消息日志文件操作
//        /// </summary>
//        private static readonly log4net.ILog _LogRunningInfo = log4net.LogManager.GetLogger("LogRunningInfo");


//        private static readonly log4net.ILog _LogWarning = log4net.LogManager.GetLogger("LogWarning");

//        private static LogHelper logcurrent = new LogHelper();
//        public static LogHelper Current 
//        {
//            get { return logcurrent; }
//        }
//        private LogHelper()
//        {
//            SetConfig();
//            IsSetConfig = true;
//        }
//        public void SetConfig()
//        {
//            string path = System.Web.HttpContext.Current.Server.MapPath("XmlConfig/log4net.config");
//            FileInfo ConfigFile = new FileInfo(path);
//            log4net.Config.XmlConfigurator.Configure(ConfigFile);
//        }

//        public void Error(Exception ex)
//        {
//            Error("执行报错", ex);
//        }
//        public void Error(object msg)
//        {
//            //_LogError.Error("异常信息（ZIT.Utilities）：" + msg);
//            Error(msg.ToString(), null);
//        }
//        /// <summary>
//        /// 写异常日志函数
//        /// </summary>
//        /// <param name="strMsg"></param>
//        /// <param name="ex"></param>
//        public void Error(string strMsg, Exception ex)
//        {
//            _LogError.Error("异常信息（ZIT.Utilities）：" + strMsg, ex);
//        }

//        public void LogInfo(string meg)
//        {
//            WriteLog(meg, LogType.Running);
//        }
//        public void LogWarn(string meg)
//        {
//            WriteLog(meg, LogType.Warning);
//        }

//        /// <summary>
//        /// 写指定类型的消息日志
//        /// </summary>
//        /// <param name="strLevelMsg"></param>
//        /// <param name="IsHighLevel"></param>
//        private void WriteLog(string strLevelMsg, LogType lt)
//        {
//            switch (lt)
//            {
//                case LogType.Error:
//                    {
//                        _LogError.Error("异常信息："  + strLevelMsg);
//                        break;
//                    }
//                case LogType.Warning:
//                    {
//                        _LogWarning.Warn(strLevelMsg);
//                        break;
//                    }
//                case LogType.Running:
//                    {
//                        _LogRunningInfo.Info(strLevelMsg);
//                        break;
//                    }


//                default:
//                    break;
//            }



//        }

//    }



//}
